<template>
	<el-form class="form"  :model="form" ref="formRef" :rules="rules" label-width="120px"  size="small">
		<div class="form_box">
			<el-row>
				<el-col :span="12">
					<el-form-item label="角色名称:" prop="name">
						<el-input v-model="form.name" placeholder="" ></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="排序:" prop="sortOrder">
            <el-input type="number" v-model="form.sortOrder"  placeholder=""  ></el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="是否所有权限:" prop="hasAllRight">
						<el-select v-model="form.hasAllRight" filterable>
							<el-option v-for="item in yesOrNotList" :key="item.value" :label="item.label" :value="item.value">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="备注:" prop="remark">
						<el-input v-model="form.remark" placeholder="" ></el-input>
					</el-form-item>
				</el-col>

				<el-col :span="12">
					<el-form-item label="状态:" prop="status">
						<el-select v-model="form.status" filterable>
							<el-option v-for="item in statusList" :key="item.value" :label="item.label" :value="item.value">
							</el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
		</div>
	</el-form>
</template>
<script>
import global_ from "@/utils/GlobalDict";
import { modifySysRole,addSysRole,checkRole } from "@/api/system/sysRole";
export default {
	props: {
		dataObj: {
			type: Object,
			default: new Object()
		},
	},
	filters: {
	},
	data(){
		return {
			form:new Object(),
      yesOrNotList:global_.elSelectConverter(global_.yesOrNot),
			statusList:global_.elSelectConverter(global_.status),
			rules:{
				name:[{required: true, message: '请填写角色名称', trigger: 'blur'},],
				sortOrder:[{required: true, message: '请填写排序', trigger: 'blur'},],
				status:[{required: true, message: '请填写状态', trigger: 'blur'},],
			},
		}
	},
	methods:{
		save(){
			this.$refs['formRef'].validate((valid) => {
				if(valid) {
					this.doSave();
				}else{
					return null;
				}
			});
		},
		async doSave(){
		  var result = 0;
		  await checkRole({id:this.form.id,name:this.form.name}).then(response=>{
        result = response.data;
      });
		  if(result==-1){
        this.$alert('角色名已存在', '提示', {
          confirmButtonText: '确定',
          callback: action => {}
        });
        return;
      }else if(result==-99){
        this.$alert('系统异常', '提示', {
          confirmButtonText: '确定',
          callback: action => {}
        });
        return;
      }
			if(this.form.id){
				modifySysRole(this.form).then(response => {
					this.saveCallBack(response);
				});
			}else{
				addSysRole(this.form).then(response => {
					this.saveCallBack(response);
				});
			}
		},
		saveCallBack(response){
			if(response.data.success){
				this.$notify({
					title: '提示',
					message: '操作成功',
					type: 'success',
					duration: 2000
				})
				this.closeDialog(1)
			}else{
				this.$notify({
					title: '提示',
					message: '操作失败',
					type: 'danger',
					duration: 2000
				})
			}
		},
		closeDialog(reload){
			this.$emit("closeDialog",reload);
		},
	},
	created(){
		this.form = this.dataObj;
		if(this.dataObj && this.dataObj.id){
      this.form.createTime = null;
      this.form.modifyTime = null;
    }else{
      this.form.status = 1;
    }
	},
}
</script>
